local huoshou = fk.CreateSkill {

  name = "joyex__huoshou",

  tags = { Skill.Compulsory, },

}



huoshou:addEffect(fk.PreCardEffect, {
  name = "joyex__huoshou",
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  events = {fk.PreCardEffect, fk.TargetSpecified,fk.EventPhaseStart,fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      if target == player and player.phase == Player.Play then
        return true
      else
        return false
      end
    end
    if player:hasSkill(huoshou.name) and  data.card and data.card.trueName == "savage_assault" then
      if event == fk.PreCardEffect then
        return player.id == data.to
      elseif event ==  fk.TargetSpecified then
        return target ~= player and data.firstTarget
      elseif event == fk.CardUsing then
        return target == player and player.phase == Player.Play
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.PreCardEffect then
      return true
    elseif event == fk.EventPhaseStart then
      local cards = room:getCardsFromPileByRule("savage_assault",1,"DiscardPile")
    if #cards > 0 then
      room:obtainCard(player,cards,true,fk.ReasonPrey,player.id,huoshou.name)
    end
    elseif event == fk.CardUsing then 
      room:setPlayerMark(player,"@@joyex__huoshou-phase",1)
    else
      data.extra_data = data.extra_data or {}
      data.extra_data.huoshou = player.id
      if not player.dead then
        player:drawCards(1,huoshou.name)
      end
    end
  end,
})
huoshou:addEffect(fk.TargetSpecified, {
  name = "joyex__huoshou",
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  events = {fk.PreCardEffect, fk.TargetSpecified,fk.EventPhaseStart,fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      if target == player and player.phase == Player.Play then
        return true
      else
        return false
      end
    end
    if player:hasSkill(huoshou.name) and  data.card and data.card.trueName == "savage_assault" then
      if event == fk.PreCardEffect then
        return player.id == data.to
      elseif event ==  fk.TargetSpecified then
        return target ~= player and data.firstTarget
      elseif event == fk.CardUsing then
        return target == player and player.phase == Player.Play
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.PreCardEffect then
      return true
    elseif event == fk.EventPhaseStart then
      local cards = room:getCardsFromPileByRule("savage_assault",1,"DiscardPile")
    if #cards > 0 then
      room:obtainCard(player,cards,true,fk.ReasonPrey,player.id,huoshou.name)
    end
    elseif event == fk.CardUsing then 
      room:setPlayerMark(player,"@@joyex__huoshou-phase",1)
    else
      data.extra_data = data.extra_data or {}
      data.extra_data.huoshou = player.id
      if not player.dead then
        player:drawCards(1,huoshou.name)
      end
    end
  end,
})
huoshou:addEffect(fk.EventPhaseStart, {
  name = "joyex__huoshou",
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  events = {fk.PreCardEffect, fk.TargetSpecified,fk.EventPhaseStart,fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      if target == player and player.phase == Player.Play then
        return true
      else
        return false
      end
    end
    if player:hasSkill(huoshou.name) and  data.card and data.card.trueName == "savage_assault" then
      if event == fk.PreCardEffect then
        return player.id == data.to
      elseif event ==  fk.TargetSpecified then
        return target ~= player and data.firstTarget
      elseif event == fk.CardUsing then
        return target == player and player.phase == Player.Play
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.PreCardEffect then
      return true
    elseif event == fk.EventPhaseStart then
      local cards = room:getCardsFromPileByRule("savage_assault",1,"DiscardPile")
    if #cards > 0 then
      room:obtainCard(player,cards,true,fk.ReasonPrey,player.id,huoshou.name)
    end
    elseif event == fk.CardUsing then 
      room:setPlayerMark(player,"@@joyex__huoshou-phase",1)
    else
      data.extra_data = data.extra_data or {}
      data.extra_data.huoshou = player.id
      if not player.dead then
        player:drawCards(1,huoshou.name)
      end
    end
  end,
})
huoshou:addEffect(fk.CardUsing, {
  name = "joyex__huoshou",
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  events = {fk.PreCardEffect, fk.TargetSpecified,fk.EventPhaseStart,fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    if event == fk.EventPhaseStart then
      if target == player and player.phase == Player.Play then
        return true
      else
        return false
      end
    end
    if player:hasSkill(huoshou.name) and  data.card and data.card.trueName == "savage_assault" then
      if event == fk.PreCardEffect then
        return player.id == data.to
      elseif event ==  fk.TargetSpecified then
        return target ~= player and data.firstTarget
      elseif event == fk.CardUsing then
        return target == player and player.phase == Player.Play
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.PreCardEffect then
      return true
    elseif event == fk.EventPhaseStart then
      local cards = room:getCardsFromPileByRule("savage_assault",1,"DiscardPile")
    if #cards > 0 then
      room:obtainCard(player,cards,true,fk.ReasonPrey,player.id,huoshou.name)
    end
    elseif event == fk.CardUsing then 
      room:setPlayerMark(player,"@@joyex__huoshou-phase",1)
    else
      data.extra_data = data.extra_data or {}
      data.extra_data.huoshou = player.id
      if not player.dead then
        player:drawCards(1,huoshou.name)
      end
    end
  end,
})
huoshou:addEffect(fk.PreDamage, {
  refresh_events = {fk.PreDamage,fk.EventAcquireSkill},
  can_refresh = function(self, event, target, player, data)
    if event == fk.PreDamage then
      if data.card and data.card.trueName == "savage_assault" then
        local e = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
        if e then
          local use = e.data[1]
          return use.extra_data and use.extra_data.huoshou
        end
      end
    elseif event == fk.EventAcquireSkill and target == player and player:hasSkill(self,true) and player.phase == Player.Play then
      return #player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
        local use = e.data[1]
        return use.from == player.id and use.card.trueName == "savage_assault"
      end, Player.HistoryPhase) > 0
    end
  end,
  on_refresh = function(self, event, target, player, data)
    if event == fk.PreDamage then
      local room = player.room
      local e = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
      if e then
        local use = e.data[1]
        data.from = room:getPlayerById(use.extra_data.huoshou)
      end
    elseif event == fk.EventAcquireSkill then
      player.room:setPlayerMark(player,"@@joyex__huoshou-phase",1)
    end
  end,
})
huoshou:addEffect(fk.EventAcquireSkill, {
  refresh_events = {fk.PreDamage,fk.EventAcquireSkill},
  can_refresh = function(self, event, target, player, data)
    if event == fk.PreDamage then
      if data.card and data.card.trueName == "savage_assault" then
        local e = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
        if e then
          local use = e.data[1]
          return use.extra_data and use.extra_data.huoshou
        end
      end
    elseif event == fk.EventAcquireSkill and target == player and player:hasSkill(self,true) and player.phase == Player.Play then
      return #player.room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
        local use = e.data[1]
        return use.from == player.id and use.card.trueName == "savage_assault"
      end, Player.HistoryPhase) > 0
    end
  end,
  on_refresh = function(self, event, target, player, data)
    if event == fk.PreDamage then
      local room = player.room
      local e = room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
      if e then
        local use = e.data[1]
        data.from = room:getPlayerById(use.extra_data.huoshou)
      end
    elseif event == fk.EventAcquireSkill then
      player.room:setPlayerMark(player,"@@joyex__huoshou-phase",1)
    end
  end,
})

huoshou:addEffect("prohibit", {
  name = "#joyex__huoshou_prohibit",
  frequency = Skill.Compulsory,
  prohibit_use = function(self, player, card)
    return player:hasSkill(huoshou.name) and player:getMark("@@joyex__huoshou-phase") > 0 and  card.name == "savage_assault"
  end,
})

return huoshou